<template>
	<view class="root">
		<!-- 搜索商品 -->
		<goods-search bgcolor="#c00000" :radius="18" @searchClick="toSearch"></goods-search>
		<view class="cate">
			<scroll-view scroll-y="true" :style="{height:deviceHeight+'px'}" class="cate-list">
				<view v-for="(item, i) in cateList" :key="i" :class="['cate-item', active === i?'active':'']"
					@click="cateHandler(item.children,i)">{{item.cat_name}}</view>
			</scroll-view>
			<scroll-view scroll-y="true" :style="{height:deviceHeight+'px'}" class="detail" :scroll-top="scrollTop">
				<view v-for="(item, i) in detail" :key="i" class="detail-box">
					<view class="two-title">/ {{item.cat_name}} /</view>
					<view class="three-data">
						<view v-for="(item2, i2) in item.children" class="three-item" @click="threeLevelHandler(item2.cat_id)">
							<image :src="item2.cat_icon"></image>
							<text>{{item2.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	
	export default {
		mixins:[badgeMix],
		data() {
			return {
				cateList: [], //分类数据
				detail: [], //二三级数据
				active: 0, //选中标识
				deviceHeight: 0, //设备可用高度
				scrollTop:0 //滚动高度
			};
		},
		onLoad() {
			//获取设备可用高度 50为搜索区域的高度
			const info = uni.getSystemInfoSync()
			this.deviceHeight = info.windowHeight - 50
			//获取数据
			this.cateList = this.getCateList();
		},
		methods: {
			async getCateList() {
				const {
					data
				} = await uni.$http.get("/api/public/v1/categories");
				if (data.meta.status !== 200) {
					return uni.$showMsg();
				}
				this.cateList = data.message;
				this.detail = data.message[0].children;
				console.log(data.message[0].children);
			},
			cateHandler(data, i) {
				this.active = i;
				this.detail = data
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			threeLevelHandler(cat_id){
				uni.navigateTo({
					url:"/subpkg/goods_list/goods_list?cid="+cat_id
				})
			},
			toSearch(){
				uni.navigateTo({
					url:"/subpkg/search/search"
				})
			}
		}

	}
</script>

<style lang="scss">
	.cate {
		display: flex;
		background-color: #fff;
		.cate-list {
			width: 120px;
			.cate-item {
				line-height: 60px;
				background-color: #f7f7f7;
				text-align: center;
				font-size: 12px;

				&.active {
					position: relative;
					background-color: #fff;

					&::before {
						content: ' ';
						position: absolute;
						top: 50%;
						transform: translateY(-50%);
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
					}
				}
			}
		}

		.two-title {
			padding: 15px 0;
			text-align: center;
			font-size: 12px;
			font-weight: bolder;
		}
		.three-data{
			display: flex;
			flex-wrap: wrap;
			.three-item{
				display: flex;
				flex-direction: column;
				align-items: center;
				width: 33.33%;
				padding: 10rpx 0;
				image{
					width: 60px;
					height: 60px;
				}
				text{
					font-size: 12px;
					font-weight: normal
				}
			}
		}
	}
</style>